package com.lucene.blog.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.lucene.blog.bean.Comment;
import com.lucene.blog.bean.PageBean;
import com.lucene.blog.db.service.CommentService;
import com.lucene.blog.util.DateJsonValueProcessor;
import com.lucene.blog.util.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

/**
 * 后台评论Controller层
 * @author yachao
 *
 */
@Controller
@RequestMapping("/admin/comment")
public class CommentAdminController {
	
	@Resource
	private CommentService commentService;
	
	/**
	 * 分页查询评论信息
	 * @param page
	 * @param rows
	 * @param state
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/listComment")
	public String listBlog(@RequestParam(value="page", required=false) String page,
						   @RequestParam(value="rows", required=false) String rows,
						   @RequestParam(value="state", required=false) String state,
						   HttpServletResponse response) throws Exception {
		PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("pageBegin", pageBean.getPageBegin());
		condition.put("pageSize", pageBean.getPageSize());
		condition.put("state", state);
		
		List<Comment> commentList = commentService.getCommentData(condition);
		Long total = commentService.getTotal(condition);
		
		JSONObject result = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor("yyyy-MM-dd"));
		JSONArray jsonArray = JSONArray.fromObject(commentList, jsonConfig);
		
		result.put("rows", jsonArray);
		result.put("total", total);
		ResponseUtil.write(response, result);
		return null;
	}
	
	/**
	 * 审核评论
	 * @param ids
	 * @param state
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/review")
	public String review(@RequestParam(value="ids", required=false) String ids,
						 @RequestParam(value="state", required=false) Integer state,
						 HttpServletResponse response) throws Exception {
		String[] idsStr = ids.split(",");
		JSONObject result = new JSONObject();
		for(int i = 0; i < idsStr.length; i++) {
			Comment comment = new Comment();
			comment.setCommentId(Integer.parseInt(idsStr[i]));
			comment.setState(state);
			commentService.updateCommentState(comment);
		}
		result.put("success", true);
		ResponseUtil.write(response, result);
		return null;
	}
	
	/**
	 * 删除评论信息
	 * @param ids
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/deleteComment")
	public String deleteComment(@RequestParam(value="ids", required=false) String ids,
								HttpServletResponse response) throws Exception {
		String[] idsStr = ids.split(",");
		for (int i = 0; i < idsStr.length; i++) {
			int commentId = Integer.parseInt(idsStr[i]);
			commentService.deleteCommentById(commentId);
		}
		JSONObject result = new JSONObject();
		result.put("success", true);
		ResponseUtil.write(response, result);
		return null;
	}
}
